package com.sdcqjy.property.widget;

import android.app.ProgressDialog;
import android.content.Context;

import com.sdcqjy.property.R;

/**
 * Created by 磁磁帅 on 2018/6/12.
 * 功能：
 */

public class AppDownloadDialog extends ProgressDialog {
    public AppDownloadDialog(Context context) {
        super(context);

        setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setMax(100);
        String quxiao = context.getString(R.string.quxiao);
//        setButton(DialogInterface.BUTTON_NEGATIVE, quxiao, new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//            }
//        });
    }

    public void onStart() {
        super.onStart();
        if (onDownloadDialogSet != null)
            onDownloadDialogSet.setDownloadListener(onDownloadListener);
    }

    protected void onStop() {
        super.onStop();
        if (onDownloadDialogSet != null)
            onDownloadDialogSet.setDownloadListener(null);
        onDownloadDialogSet = null;
    }

    private OnDownloadListener onDownloadListener = (progress, total) -> {
        int v = (int) (progress * 100.0 / total);
        setProgress(v);
    };

    private OnDownloadDialogSet onDownloadDialogSet;

    public AppDownloadDialog setOnDownloadDialogSet(OnDownloadDialogSet onDownloadDialogSet) {
        this.onDownloadDialogSet = onDownloadDialogSet;
        return this;
    }

    public interface OnDownloadDialogSet {
        public void setDownloadListener(OnDownloadListener onDownloadListener);
    }

    public interface OnDownloadListener {
        public void onDownloadListener(long progress, long total);
    }
}
